﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;

namespace SSCCMembership.Web.Services.Members
{
    [ErrorHandlerBehavior]
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class PersonalInfoService
    {
        [OperationContract]
        public PersonalInfoData LoadPersonalInfo(int MemberID)
        {
            var PID = new PersonalInfoData();
            PID.ID = MemberID;
            using (var Q = SSCCDatabase.Query("SELECT FirstName, Surname, Address1, Address2, Address3, Town, County, PostCode, IsMale, DoB, TownOptOut, ImageOptOut FROM SSCC_DAT_Member WHERE (ID = @ID)",
                new SqlParameter("@ID", SqlDbType.Int), MemberID))
            {
                var M = Q.First();
                PID.FirstName = M["FirstName"].ToString();
                PID.Surname = M["Surname"].ToString();
                PID.Address1 = M["Address1"].ToString();
                PID.Address2 = M["Address2"].ToString();
                PID.Address3 = M["Address3"].ToString();
                PID.Town = M["Town"].ToString();
                PID.County = M["County"].ToString();
                PID.PostCode = M["PostCode"].ToString();
                PID.IsMale = M["IsMale"].Nullable<bool?>();
                PID.DoB = M["DoB"].Nullable<DateTime?>();
                PID.TownOptOut = (bool)M["TownOptOut"];
                PID.ImageOptOut = (bool)M["ImageOptOut"];
            }

            return PID;
        }
        [OperationContract]
        public void UpdatePersonalInfo(int MemberID, string PropertyName, object PropertyValue)
        {
            SqlParameter P = new SqlParameter();
            switch (PropertyName)
            {
                case "FirstName":
                case "Surname":
                case "Address1":
                case "Address2":
                case "Address3":
                case "Town":
                case "County":
                case "PostCode":
                    P = new SqlParameter("@value", SqlDbType.VarChar);
                    break;
                case "DoB":
                    P = new SqlParameter("@value", SqlDbType.Date);
                    break;
                case "IsMale":
                case "TownOptOut":
                case "ImageOptOut":
                    P = new SqlParameter("@value", SqlDbType.Bit);
                    break;
                default:
                    return;
            }
            SSCCDatabase.ExecuteStatement("UPDATE SSCC_DAT_Member SET " + PropertyName + "=@value WHERE ID=@ID"
                , new Dictionary<SqlParameter, object>()
                {
                    {P, PropertyValue.ToDBNullable()},
                    {new SqlParameter("@ID", SqlDbType.Int), MemberID}
                });
        }

        public class PersonalInfoData
        {
            public int ID { get; set; }

            public string FirstName { get; set; }
            public string Surname { get; set; }

            public string Address1 { get; set; }
            public string Address2 { get; set; }
            public string Address3 { get; set; }
            public string Town { get; set; }
            public string County { get; set; }
            public string PostCode { get; set; }

            public bool? IsMale { get; set; }

            public DateTime? DoB { get; set; }

            public bool TownOptOut { get; set; }
            public bool ImageOptOut { get; set; }
        }
        
        
    }
}
